<?php
    include '../connectdb.php';
    if (isset($_GET["edit"])) {
        $q = "select booking.*, customer.customer_fname, customer.customer_lname from booking, customer where booking.customer_id=customer.customer_id and booking.booking_id=".$_GET['booking_id'];
        $result = mysqli_query($link, $q);
        $row = mysqli_fetch_array($result);
        $total = $row['total_price'];
        $deposit = $row['deposit'];
        $remain = $total - $deposit;
        $q = "select min(booking_detail.date) as mindate, max(booking_detail.date) as maxdate, status_id from booking_detail where booking_id=".$_GET['booking_id'];
        $res = mysqli_query($link, $q);
        $r = mysqli_fetch_array($res);
        $action = 'edit';
        $cod = date ("Y-m-d", strtotime("+1 day", strtotime($r['maxdate'])));
        $status = $r['status_id'];
        $q = "select * from status";
        $res2 = mysqli_query($link, $q);
        $st = array();
        while ($st[] = mysqli_fetch_array($res2));
        array_pop($st);
?>
<form action="main.php?do_checkout=true" method="POST">
<input type="hidden" name="booking_id" value="<?php echo $_GET["booking_id"] ?>" />
<table border="1" width="764">
    <tr>
        <td colspan="2" align="center"><h3>Booking Management</h3></td>
    </tr>
    <tr>
        <td>id</td>
        <td><?php echo $row['booking_id'] ?></td>
    </tr>
    <tr>
        <td>Customer</td>
        <td><?php echo $row['customer_fname'].' '.$row['customer_lname'] ?></td>
    </tr>
    <tr>
        <td>Booking Date</td>
        <td><?php echo $row['booking_date'] ?></td>
    </tr>
    <tr>
        <td>Check-in</td>
        <td><?php echo $r['mindate'] ?></td>
    </tr>
    <tr>
        <td>Check-out</td>
        <td><?php echo $cod ?></td>
    </tr>
    <tr>
        <td>Total</td>
        <td><?php echo $total ?></td>
    </tr>
    <tr>
        <td>Paid</td>
        <td><?php echo $deposit ?></td>
    </tr>
    <tr>
        <td>Remain</td>
        <td><?php echo $remain ?></td>
    </tr>
    <tr>
        <td>Paid Date</td>
        <td><?php echo $row['deposit_date'] ?></td>
    </tr>
    <tr>
        <td colspan="2"> -- addition fee</td>
    </tr>
    <?php
        $q = "select * from service_type order by service_id";
        $sRes = mysqli_query($link, $q);
        while ($row = mysqli_fetch_array($sRes)) {
    ?>
    <tr>
        <td><?php echo $row['service_name'] ?></td>
        <td>
            <input type="text" name="sInput-<?php echo $row['service_id'] ?>" >
        </td>
    </tr>
    <?php
        }
    ?>
    <tr>
        <td colspan="2" align="center"><input type="submit" value="Check-out" /> <input type="reset" value="reset"/></td>
    </tr>
</table>
</form>
<?php
    } else {
?>
<table border="1" width="764">
    <tr>
        <td colspan="11" align="center"><h3>Check-out</h3></td>
    </tr>
    <tr>
        <td>id</td>
        <td>Customer</td>
        <td>Booking Date</td>
        <td>Check-in</td>
        <td>Check-out</td>
        <td>Total</td>
        <td>Paid</td>
        <td>Paid Date</td>
        <td>Remain</td>
        <td>Status</td>
        <td>edit</td>
    </tr>
    <?php
        $q = "select booking.*, customer.customer_fname, customer.customer_lname from booking, customer where booking.customer_id=customer.customer_id";
        $result = mysqli_query($link, $q);
        while ($row = mysqli_fetch_array($result)){ 
            $total = $row['total_price'];
            $deposit = $row['deposit'];
            $remain = $total - $deposit;
            $q = "select min(booking_detail.date) as mindate, max(booking_detail.date) as maxdate, status_name, booking_detail.status_id from booking_detail, status where status.status_id=booking_detail.status_id and booking_id=".$row['booking_id'];
            $res = mysqli_query($link, $q);
            $r = mysqli_fetch_array($res);
            $cod = date ("Y-m-d", strtotime("+1 day", strtotime($r['maxdate'])));
            $status = $r['status_name'];
            if ($r['status_id'] == 3) {
    ?>
    <tr>
        <td><a href="#"><?php echo $row['booking_id'];?></a></td>
        <td><?php echo $row['customer_fname'].' '.$row['customer_lname'];?></td>
        <td><?php echo $row['booking_date'];?></td>
        <td><?php echo $r['mindate'];?></td>
        <td><?php echo $cod;?></td>
        <td><?php echo $total;?></td>
        <td><?php echo $deposit;?></td>
        <td><?php echo $row['deposit_date'];?></td>
        <td><?php echo $remain;?></td>
        <td><?php echo $status;?></td>
        <td><a href="main.php?checkout=true&edit=true&booking_id=<?php echo $row['booking_id'];?>">check-out</a></td>
    </tr>
    <?php
            }
        }
    ?>
</table>
<?php
    }
?>